<?php
if( !class_exists('mysqldb') )
{
	require_once(dirname(__FILE__)."/../config.inc.php");
	include_once(CFG_LIB_DIR.'mysqldb.inc.php');
}

class Buy_CS extends mysqldb
{
	//添加商品Car_Add(产品id,购买数量,会员ID)
	function Car_Add($P_ID=0,$P_Num=0,$P_UID=0,$PType=0)
	{
		if($P_ID==0 || $P_Num==0){return false;}
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		/*$strSql="select * from hy_b_products where SysProductsID='".$P_ID."' ";
		parent::query($strSql);
		$rs = parent::get_data();*/
		if($PType==0)
		{
			$rs=$this->GetProductInfo($P_ID);
		}
		else
		{
			$rs=$this->GetSndInfo($P_ID);
		}
		if($rs){
			$strSql="insert into `hy_b_buycar`(`UID`,`PID`,`ProductName`,`ProductSize`,`ProductContainment`,`ProductPrice`,`BuyNumber`,`State`,Photo,FromMemberID) values('".$P_UID."','".$P_ID."','".$rs[0]['ProductName']."','".$rs[0]['Size']."','".$rs[0]['containment']."','".$rs[0]['MemberPrice']."','".$P_Num."','0','".$rs[0]['FirstPhoto']."','".$rs[0]['MemberID']."')";
			//exit($strSql);
			$blnResult=parent::query($strSql);
			return $blnReSult;
		}
		else{
			return false;
		}
	}
	
	//修改商品数量Car_Edit(产品id,购买数量)
	function Car_Edit($P_ID=0,$P_Num=0)
	{
		if($P_ID==0 || $P_Num==0){return false;}
		$strSql="update hy_b_buycar set BuyNumber='".$P_Num."' where ID='".$P_ID."' ";
		$blnResult=parent::query($strSql);
		return $blnReSult;
	}
	
	//获取用户IP作为会员ID
	function getUserStrIP()
	{
		return str_replace(".","",$_SERVER['REMOTE_ADDR']);
	}
	
	//删除商品Car_Del(产品id[全部删除为all],会员ID)
	function Car_Del($P_ID="0",$P_UID=0)
	{
		if($P_ID=="0"){return false;}
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		if($P_ID=="all"){
			$strSql="delete from hy_b_buycar where UID='".$P_UID."' and State=0 ";
		}
		else{
			$strSql="delete from hy_b_buycar where ID='".$P_ID."' ";
		}
		$blnResult=parent::query($strSql);
		return $blnReSult;
	}
	
	//下单成功时修改购物车商品状态Car_Edit_State(产品id[全部修改为all],会员ID,订单号)
	function Car_Edit_State($P_ID="0",$P_UID=0,$P_OID=0)
	{
		if($P_ID=="0"){return false;}
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		if($P_ID=="all"){
			$strSql="update hy_b_buycar set State=1,OID='".$P_OID."' where UID='".$P_UID."' and State=0 ";
		}
		else{
			$strSql="update hy_b_buycar set State=1,OID='".$P_OID."' where ID='".$P_ID."' ";
		}
		$blnResult=parent::query($strSql);
		return $blnReSult;
	}
	
	//获取购物车信息GetBuyCar(会员ID)
	function GetBuyCar($P_UID=0)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select * from hy_b_buycar where UID='".$P_UID."' and State=0 order by ID desc";
		//exit($strSql);
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//检查获取购物车是否存在该记录CheckBuyCarProduct(产品ID,会员ID,产品0还是供应1)
	function CheckBuyCarProduct($P_ID=0,$P_UID=0,$P_PType=0)
	{
		if($P_ID==0){return false;}
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select * from hy_b_buycar where UID='".$P_UID."' and State=0 and PID='".$P_ID."' and CarType='".$P_PType."' order by ID desc";
		//exit($strSql);
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//获取订单产品信息GetOrderProduct(订单ID)
	function GetOrderProduct($P_OID=0)
	{
		if($P_OID==0){return false;}
		$strSql="select b.*,m.MemberName from hy_b_buycar b left join hy_b_member m on b.FromMemberID=m.MemberID where b.OID='".$P_OID."' order by b.ID desc";
		//echo $strSql;
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//添加订单信息AddOrder(会员ID,字段数组)
	function AddOrder($P_UID=0,$parameter)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$fltOrderMoney=$parameter['ProductMoney']+$parameter['SendMoney'];
		$strSql="insert into `hy_b_buyorder`(`ID`,`UID`,`Price`,`SendType`,`SendTypeName`,`SendMoney`,`PayType`,`PayTypeName`,`State`,`PersonName`,`Telephoto`,`Mobile`,`Email`,`Address`,`PostCode`,`Message`,`CreateTime`,`IsGetFaPiao`,`FaPiaoType`,`FaPiaoHead`,`FaPiaoContent`) values('".$parameter['OrderID']."','".$P_UID."','".$fltOrderMoney."','".$parameter['SendType']."','".$parameter['SendTypeName']."','".$parameter['SendMoney']."','".$parameter['PayType']."','".$parameter['PayTypeName']."','0','".$parameter['PersonName']."','".$parameter['Telephoto']."','".$parameter['Mobile']."','".$parameter['Email']."','".$parameter['Address']."','".$parameter['PostCode']."','".$parameter['Message']."','".time()."','".$parameter['IsGetFaPiao']."','".$parameter['FaPiaoType']."','".$parameter['FaPiaoHead']."','".$parameter['FaPiaoContent']."')";
		//exit($strSql);
		$blnResult=parent::query($strSql);
		if($blnResult){
			$this->Car_Edit_State("all",$P_UID,$parameter['OrderID']);
			return true;
		}
		else{
			return false;
		}
	}
	
	//获取商号信息GetPayPartnerInfo(主键ID)
	function GetPayPartnerInfo($P_ID=0)
	{
		$strSql="select * from hy_b_pay_partner_info where ID='".$P_ID."' ";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//修改商号信息EditPayPartnerInfo(数组)
	function EditPayPartnerInfo($parameter)
	{
		$strSql="update hy_b_pay_partner_info set PartnerID='".$parameter['PartnerID']."',SecurityCode='".$parameter['SecurityCode']."',SellerEmail='".$parameter['SellerEmail']."' where ID='".$parameter['ID']."' ";
		$blnReSult=parent::query($strSql);
		return $blnReSult;
	}
	
	//获取配送方式
	function GetSendType($cfg_cur_lan)
	{
		$strSql="select * from hy_b_sendtype where Lang = '".$cfg_cur_lan."' order by ID asc ";
		parent::query($strSql);
		$rs = parent::get_data();
		//echo $strSql; exit;
		return $rs;
	}
	
	//获取付款方式
	function GetPayType($cfg_cur_lan)
	{
		$strSql="select * from hy_b_paytype where Lang = '".$cfg_cur_lan."' order by ID asc ";
		parent::query($strSql);
		$rs = parent::get_data();
		//echo $strSql; exit;
		return $rs;
	}
	
	//获取会员信息GetMemberInfo(会员ID)
	function GetMemberInfo($P_UID=0)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select * from hy_b_member where MemberID='".$P_UID."'";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//检查会员名CheckMemberName(会员ID)
	function CheckMemberName($P_UName="")
	{
		if($P_UName==""){return false;}
		$strSql="select * from hy_b_member where MemberName='".$P_UName."'";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
		
	//获取购物车中产品的总价格GetBuyMoney(会员ID)
	function GetBuyMoney($P_UID=0)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select sum(ProductPrice*BuyNumber) as Price from hy_b_buycar where State=0 and UID='".$P_UID."' ";
		//echo $strSql;
		parent::query($strSql);
		$rs = parent::get_data();
		if($rs[0]['Price']>0){$Price=$rs[0]['Price'];}
		else{$Price=0;}
		return $Price;
	}
	
	//获取购物车数量GetBuyCar(会员ID)
	function GetBuyCarCount($P_UID=0)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select count(*) as counter from hy_b_buycar where State=0 and UID='".$P_UID."' ";
		//echo $strSql;
		parent::query($strSql);
		$rs = parent::get_data();
		if($rs[0]['counter']>0){$intCounter=$rs[0]['counter'];}
		else{$intCounter=0;}
		return $intCounter;
	}
	
	//获取产品信息GetProductInfo(产品ID)
	function GetProductInfo($P_ID=0)
	{
		if($P_ID==0){return false;}
		$strSql="select * from hy_b_products where ProductsID='".$P_ID."'";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//获取供应信息GetSndInfo(产品ID)
	function GetSndInfo($P_ID=0)
	{
		if($P_ID==0){return false;}
		$strSql="select SndTitle as ProductName,MemberID,FirstPhoto,SndMemberPrice as MemberPrice,'' as Size,'' as containment,CateID from hy_b_supplyndemand where SndID='".$P_ID."'";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//获取会员订单GetMemberOrder(会员ID)
	function GetMemberOrder($P_UID=0,$P_OID="")
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select b.*,m.MemberName from hy_b_buyorder b left join hy_b_member m on b.UID=m.MemberID where b.UID='".$P_UID."' ";
		if($P_OID!=""){$strSql .=" and b.ID='".$P_OID."' ";}
		$strSql .=" order by b.ID desc";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//获取会员已售产品GetMemberSellProduct(会员ID)
	function GetMemberSellProduct($P_UID=0)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select b.ID as CarID,b.Ispay as CarIspay,b.ProductName as CarProductName,b.PID as CarPID,b.ProductPrice as CarProductPrice,b.ID as CarProductID,b.IsSended as CarIsSended,o.*,m.MemberName from hy_b_buycar b inner join hy_b_buyorder o on b.OID=o.ID left join hy_b_member m on b.UID=m.MemberID where b.State=1 and b.FromMemberID='".$P_UID."' order by b.ID desc ";
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//产生订单号
	function GetOrderID()
	{
		$MaxID=1;
		$strSql="select max(ID)+1 as MaxID from hy_b_buycar ";
		parent::query($strSql);
		$rs = parent::get_data();
		if($rs){$MaxID=$rs[0]['MaxID'];}
		$ID=date("Ymdhis",time()).$this->random(4,1).$MaxID;
		return $ID;
	}
	
	//查看收藏夹是否有记录
	function CheckFavorites($P_UID,$P_ID=0,$P_Type="pro")
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select * from hy_b_member_Favorites where MemberID='".$P_UID."' and InfoID='".$P_ID."' and FType='$P_Type' ";
		//exit($strSql);
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//获取收藏夹
	function GetFavorites($P_UID)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select * from hy_b_member_Favorites where MemberID='".$P_UID."' order by ID desc ";
		//exit($strSql);
		parent::query($strSql);
		$rs = parent::get_data();
		return $rs;
	}
	
	//收藏夹删除
	function DelFavorites($P_ID)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="delete from hy_b_member_Favorites where ID='".$P_ID."' ";
		//exit($strSql);
		$rs=parent::query($strSql);
		return $rs;
	}
	
	//获取收藏夹记录数
	function GetFavoritesCount($P_UID)
	{
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		$strSql="select count(*) as counter from hy_b_member_Favorites where MemberID='".$P_UID."' ";
		//exit($strSql);
		parent::query($strSql);
		$rs = parent::get_data();
		$intCounter=0;
		if($rs){$intCounter=$rs[0]['counter'];}
		return $intCounter;
	}
	
	//加入到收藏夹Favorites_Add(会员ID,信息的ID,信息类型（产品、商机...）);返回结果：0为该记录已经存在,1为添加失败,2为添加成功
	function Favorites_Add($P_UID,$P_ID=0,$P_Type="pro")
	{
		if($P_ID==0){return false;}
		if($P_UID==0){$P_UID=$this->getUserStrIP();}
		if($P_Type=="pro")
		{
			$rs=$this->GetProductInfo($P_ID);
		}
		else if($P_Type=="snd")
		{
			$rs=$this->GetSndInfo($P_ID);
		}
		else
		{
			return false;
		}
		$blnReSult=$this->CheckFavorites($P_UID,$P_ID,$P_Type);
		if($blnReSult)
		{
			return 0;
		}
		else
		{
			$strSql="insert into  hy_b_member_Favorites(`MemberID`,`InfoID`,`Name`,`CateID`,`CreateDate`,`FType`) values('$P_UID','$P_ID','".$rs[0]['ProductName']."','".$rs[0]['CateID']."',now(),'$P_Type')";
			$blnResult=parent::query($strSql);
			if($blnResult)
			{
				return 2;
			}
			else
			{
				return 1;
			}
		}
	}
	
	//获取随机数
	function random($length, $numeric = 0)
	{
		mt_srand((double)microtime() * 1000000);
		if($numeric) {
			$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
		} else {
			$hash = '';
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
			$max = strlen($chars) - 1;
			for($i = 0; $i < $length; $i++) {
				$hash .= $chars[mt_rand(0, $max)];
			}
		}
		return $hash;
	}
	

}

?>

